﻿ 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Toolkit.Converter
{
    /// <summary>
    /// 和参数比较的转换器
    /// </summary>
    /// <typeparam name="Target"></typeparam>
    public abstract class ComparableParameterConverter<Target> : ConverterBase<object, Target>
    {
        /// <summary>
        /// 指定匹配的符号
        /// </summary>
        public Sign MatchSign { set; get; }

        /// <summary>
        /// 指定匹配的参数
        /// </summary>
        public object MatchParameter { set; get; }

        /// <summary>
        /// 错误的代号
        /// </summary>
        public const int Error = -200;

        protected override Target ConvertNotNull(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object compareValue = GetCompareValue(value);
            object compareParameter = GetCompareParameter(parameter);
            if (compareParameter == null)
            {
                if(MatchParameter != null)
                {
                  compareParameter = MatchParameter;
                }
                else
                {
                    return this.GetValueFromComparabled(false, Error);
                }
            }
            bool isMatch = false;
            IComparable iValue;
            IComparable iParameter;
            int retVal;
            if (compareValue is IComparable && compareParameter is IComparable)
            {
                iValue = (IComparable)compareValue;
                iParameter = (IComparable)compareParameter;
            }
            else
            {
                iValue = value.ToString();
                iParameter = compareParameter.ToString();
            }
            try
            {
                retVal = iValue.CompareTo(iParameter);
            }
            catch (ArgumentException)
            {
                //如果不同类型则转换为字符串比较
                iValue = value.ToString();
                iParameter = compareParameter.ToString();
                retVal = iValue.CompareTo(iParameter);
            }
            catch 
            {
                return this.GetValueFromComparabled(false, Error);
            }
            if (MatchSign != Sign.None)
            {
                if (retVal == 0 && (MatchSign == Sign.Equals || MatchSign == Sign.GreaterThanEquals || MatchSign == Sign.LessThanEquals))
                {
                    isMatch = true;
                }
                else if (retVal > 0 && (MatchSign == Sign.NotEquals || MatchSign == Sign.GreaterThanEquals || MatchSign == Sign.GreaterThan))
                {
                    isMatch = true;
                }
                else if (retVal < 0 && (MatchSign == Sign.NotEquals || MatchSign == Sign.LessThanEquals || MatchSign == Sign.LessThan))
                {
                    isMatch = true;
                }
            }
            return this.GetValueFromComparabled(isMatch, retVal);
        }


        protected override Target GetConvertNullValue(Type targetType, object parameter)
        {
            if (parameter == null)
            {
                this.GetValueFromComparabled(true, 0);
            }
            return base.GetConvertNullValue(targetType, parameter);
        }

        protected override object ConvertBackNotNull(Target value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new InvalidOperationException();
        }

        protected abstract Target GetValueFromComparabled(bool isMatch,int result);

        protected virtual object GetCompareValue(object value)
        {
            return value;
        }

        protected virtual object GetCompareParameter(object parameter)
        {
            return parameter;
        }
    }

    /// <summary>
    /// 匹配类型
    /// </summary>
    public enum Sign
    { 
      None = 0,
      /// <summary>
      /// 等于
      /// </summary>
      Equals = 1,
      /// <summary>
      /// 不等于
      /// </summary>
      NotEquals=2,
      /// <summary>
      /// 大于
      /// </summary>
      GreaterThan = 3,
      /// <summary>
      /// 小于
      /// </summary>
      LessThan = 4,
      /// <summary>
      /// 大于等于
      /// </summary>
      GreaterThanEquals = 5,
      /// <summary>
      /// 小于等于
      /// </summary>
      LessThanEquals = 6
    }

   
}

